#!/bin/bash
# Reads module-names from MODULES and executes make in each module-directory.
buildok=""
buildfail=""
input="./MODULES"
extcmds=""

if [ $# -gt 0 ]; then
  extcmds="$*"
fi

while read line;
do
  module="`echo $line | cut -d'#' -f 1`"
  cmd="`echo $line | cut -d'#' -f 2-`"

  echo "## `date` ### Executing \"${cmd}\" in module \"${module}\""
  pushd $module &>/dev/null
  echo $cmd $extcmds
  $cmd $extcmds >&/dev/null
  if [ $? -eq 0 ]
  then
    buildok+="${module} (${cmd})"
    buildok+="\n"
    echo -ne "\e[32m"
    echo "Executing \"${cmd}\" in module \"${module}\" was succesful."
    echo -ne "\e[0m"
  else
    buildfail+="${module}(${cmd})"
    buildfail+="\n"
    echo -ne "\e[31m"
    echo "Executing \"${cmd}\" in module \"${module}\" failed!"
    echo -ne "\e[0m"
  fi
  popd &>/dev/null
done < ${input}
echo "##### `date` #####"

echo -e "\n\n"
echo "Built succesfully:"
echo -ne "\e[32m"
echo -e $buildok
echo -ne "\e[0m"

echo ""

echo "Failed to build:"
echo -ne "\e[31m"
echo -e $buildfail
echo -ne "\e[0m"

rm -f input.$$

echo -e "\n\n"
